package com.ykefu.assign.netty.netty.websocket;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelMatchers;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author likai
 * @date 2020/4/18 19:21
 * @email likai@7moor..com
 * @desc
 */
public class HandlerMessage {

    private static final Logger LOGGER = LoggerFactory.getLogger(HandlerMessage.class);

    public void sendToAll(String message){
        LOGGER.info("推送给所有人");
        ChannelManager.group.writeAndFlush(new TextWebSocketFrame(message));
    }


    public void sendToUser(String connectId,String message){
        if(connectId==null||"".equals(connectId)){
            LOGGER.error("接受人参数错误：{}",connectId);
            return;
        }
        Channel channel = ChannelManager.channels.get(connectId);
        if(channel==null){
            LOGGER.error("接收人未在线：{}",connectId);
            return;
        }
        channel.writeAndFlush(new TextWebSocketFrame(message));
    }


    private HandlerMessage(){}


    private static class InnerHandlerMessage{
        private static HandlerMessage instance = new HandlerMessage();
    }

    public static HandlerMessage getInstance(){
        return InnerHandlerMessage.instance;
    }
}
